package com.cheer.controller;

import cn.hutool.core.util.ObjectUtil;
import com.cheer.common.Page;
import com.cheer.common.Result;
import com.cheer.dto.MallProductQueryDTO;
import com.cheer.exception.GlobalExceptionHandler;
import com.cheer.exception.ServiceException;
import com.cheer.param.MallProductBookStoreParam;
import com.cheer.service.MallProductBookStoreService;
import com.cheer.vo.MallProductBookStoreVO;
import com.cheer.vo.MallProductListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Optional;

/**
 * @author cheer
 */
@Api(tags = "产品预订controller")
@RestController
@RequestMapping("/product/book/stock")
public class MallBookProductController {

    @Autowired
    private MallProductBookStoreService mallProductBookStoreService;

    @ApiOperation("产品预订详情")
    @GetMapping("/info")
    @ApiResponses({@ApiResponse(code=200, message = "", response = MallProductBookStoreVO.class)})
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result<MallProductBookStoreVO> info(Long productId) {
        return Result.success(mallProductBookStoreService.info(productId));
    }

    @ApiOperation("产品预订添加")
    @PostMapping("/add")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result add(@Valid @RequestBody MallProductBookStoreParam mallProductBookStoreParam) {
        mallProductBookStoreService.add(mallProductBookStoreParam);
        return Result.success();
    }

    @ApiOperation("产品预订修改")
    @PostMapping("/update")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result update(@Valid @RequestBody MallProductBookStoreParam mallProductBookStoreParam) {

        if(ObjectUtil.isNull(mallProductBookStoreParam.getId())){
            throw new ServiceException("未查询到当前产品！");
        }
        mallProductBookStoreService.update(mallProductBookStoreParam);
        return Result.success();
    }


    @ApiOperation("产品预订删除")
    @GetMapping("/delete")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result delete(Long id) {
        if(id == null){
            throw new ServiceException("未查询到当前产品！");
        }
        mallProductBookStoreService.delete(id);
        return Result.success();
    }


}
